package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Bill;
import com.example.demo.entity.Customer;
import com.example.demo.mapper.BillMapper;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/bill")
public class BillController {

    @Resource
    CustomerMapper customerMapper;
    @Autowired
    @Resource
    RoomMapper roomMapper;

    @Resource
    BillMapper billMapper;
    @PostMapping("/checkOut")
    public Result<?> checkOut(@RequestBody Bill bill){
        //1.删掉已入住客户信息
        customerMapper.deleteById(bill.getCustomerId());
        //2.修改房间的状态为打扫中
        roomMapper.updateState2ByRoomId(bill.getRoomId());
        //3.添加账单
        billMapper.insert(bill);
        return Result.success();
    }

    @GetMapping("/data")
    public Result<?> test(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String search) {
        LambdaQueryWrapper<Bill> wrapper = Wrappers.<Bill>lambdaQuery();
        if (StrUtil.isNotBlank(search)) wrapper.like(Bill::getCustomerName, search);
        Page<Bill> userPage = billMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }
}
